// pages/MaintainHistory/MaintainHistory.js
const httpRequest = require('../../utils/http_utils.js')
const dateutil = require('../../utils/date_util.js')
var regeneratorRuntime = require('../../utils/runtime.js');
var utils = require('../../utils/util')
const CryptoJS = require('../../utils/aes_util.js') //参数加密
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typelistoneq: "",   
    title: '历史任务',
    tabTxt: ['位置', '设备类型', '状态'],
    tab: [false, false, false],
    EquipmentLocations: '',  //选中的 地址
    EquipmentLocation: [],  // 地址集合
    stateList: [{
      TaskState: '99',
      state: '全部'
    },
    {
      TaskState: '1',
      state: '已完成'
    },
    {
      TaskState: '2',
      state: '逾期'
    },
    ],
    typeList: [],      // 1级设备列表
    typeList2: [],     // 2级设备列表
    typeList3: [],     // 3级设备列表
    SysGuid: "",   //存储选中的设备类型1级 的 rowguid
    Sys_TypeGuid: "",   //存储选中的设备类型2级 的 rowguid
    ClassifyGuid: "",   //存储选中的设备类型3级 的 rowguid
    maskFlag: false, //是否显示遮罩
    PageSize: 10, //单页数据条数
    CurrentPage: 1, //当前页码
    historyList: [], //历史任务集合
    TotalNum: 0, //数据总条数
    showIs: false, //暂无图片
    downloading: false, //加载中
    noMore: false, //没有更多了
    loadingFailed: false, //数据加载失败
    scrollTop: 0,
    TaskType: 0, //筛选类型 （0：物业维保历史任务；1、物业维当天的任务）
    TaskState: 99, //筛选状态
    btn_text: '本月', //时间筛选按钮默认本月
    menuIndex: 0, //菜单下标
    EndTime: '', //任务结束日期
    StartTime: '', //任务开始日期
    Starttime:"",
    Endtime:"",
    TaskState:"99",
    screenTop: 0, //筛选栏Top高度
    customHeight: 0, //自定义高度
  },

  // 跳转搜索
  tiaozhuan: function() {
    wx.navigateTo({
      url: '../MaintanHistorySearch/MaintanHistorySearch',
    })
  },

  //跳转详情
  JumpDetails: function(e) {
    console.log(JSON.stringify(e.currentTarget.dataset) + "------------传参")

    let TaskGuid = e.currentTarget.dataset.taskguid ;
    let EquipmentName = e.currentTarget.dataset.equipmentname;
    let RecordBeginTime = e.currentTarget.dataset.recordbegintime;
    let TaskCompleteTime = e.currentTarget.dataset.taskcompletetime;
    let EquipmentLocation = e.currentTarget.dataset.equipmentlocation;
    let TaskBeginTime = e.currentTarget.dataset.taskbegintime;
    let TaskEndTime = e.currentTarget.dataset.taskendtime;

    wx.navigateTo({
      url: '../MaintainHistoryDetails/MaintainHistoryDetails?TaskGuid=' + TaskGuid + "&EquipmentName=" + EquipmentName + "&RecordBeginTime=" + RecordBeginTime + "&TaskCompleteTime=" + TaskCompleteTime + "&EquipmentLocation=" + EquipmentLocation + "&TaskBeginTime=" + TaskBeginTime + "&TaskEndTime=" + TaskEndTime
    })
  },

  initData: async function() { //初始化数据
    let that = this;

    let Param = {
      PageSize: that.data.PageSize,
      CurrentPage: that.data.CurrentPage,
      EquipmentLocations: that.data.EquipmentLocations,
      TaskType: that.data.TaskType,
      TaskState: that.data.TaskState,
      StartTime: that.data.StartTime,
      "EndTime": that.data.EndTime,
      "SysGuid": that.data.SysGuid,
      "Sys_TypeGuid": that.data.Sys_TypeGuid,
      "ClassifyGuid": that.data.ClassifyGuid
    }
    console.log( "参数" + JSON.stringify(Param))
    let data = await utils.post(httpRequest.apiInfo.GetMaintenanceTaskHisMobileApi, Param)
    console.log(JSON.stringify(data) + '---------------------历史任务')
     
    if (this.data.CurrentPage == 1 && utils.isEmptyObject(data.data.Data)) {
      this.setData({
        showIs: true
      })
    } else if (this.data.CurrentPage != 1 && utils.isEmptyObject(data.data.Data)) {
      that.setData({
        noMore: true
      })
    } else {
      let historyListnew = data.data.Data;
      let historyList = this.data.historyList;
      console.log(historyListnew)
      // let EquipmentLocationold = this.data.EquipmentLocation;
      // let EquipmentLocation = [];
      for (let item of historyListnew) {
        // EquipmentLocationold.push(item.EquipmentLocation);
        historyList.push(item)
      }
      // EquipmentLocation = [...new Set(EquipmentLocationold)];
      // console.log(JSON.stringify(EquipmentLocation) + "地址集合")

      this.setData({
        historyList: historyList,
        TotalNum: data.data.TotalNum,
        // EquipmentLocation: EquipmentLocation,
        noMore: false,
        showIs: false
      })
    }

  },



  //上拉加载
  scrollToLower: function() {
    var that = this
    console.log(that.data.TotalNum + "_______总条数")
    var pages = Math.round((that.data.TotalNum / 10) * 100) / 100
    if (that.data.CurrentPage > pages) {
      that.setData({
        noMore: true
      })
    } else {
      that.setData({
        noMore: false,
        CurrentPage: that.data.CurrentPage + 1
      })
     
      that.initData();
    }
  },

  //隐藏遮罩  
  hidemask() {
    this.setData({
      tab: [false, false, false],
      maskFlag: false,
      EquipmentLocations: "",
      TaskState: 99,
      "SysGuid": null,  //系统ID
      "Sys_TypeGuid": null,//类别ID
      "ClassifyGuid": null, // 类型ID
      typeList2: [],
      typeList3: [],
    })
  },

  // 历史任务菜单列表
  filterTab: function(e) {
    var data = [false, false, false];
    var index = e.currentTarget.dataset.index;
    data[index] = !this.data.tab[index];
    this.setData({
      tab: data,
      maskFlag: true,
      menuIndex: index
    })
  },

  
  pickdate() {
    wx.navigateTo({
      url: '/pages/timeData/timeData?frompage=xunjian',
    })
  },

  // 选择位置 选择状态
  searchchecked: function(e) {
      //  选择位置
    if (e.currentTarget.dataset.condition == 'location') {
      this.setData({
        EquipmentLocations: e.currentTarget.dataset.equipmentlocation
      })
        //  选择状态
    }  else if (e.currentTarget.dataset.condition == 'state') {
      console.log(e.currentTarget.dataset.taskstate)
      this.setData({
        TaskState: e.currentTarget.dataset.taskstate,
      })
    }  
  },

  //重置
  reset(e) {
    // 位置重置
    if (e.currentTarget.dataset.type == 'location') {
      this.setData({
        EquipmentLocations: ''
      })
    } else if (e.currentTarget.dataset.type == 'state'){   // 状态重置
      this.setData({
        TaskState: '99'
      })
    } else{
      this.setData({
        "SysGuid": "",
        "Sys_TypeGuid": "",
        "ClassifyGuid": "",
        typeList2: [],
        typeList3: [],
      })
    } 
  },

  //确定
  confirm() {
    console.log(this.data.menuIndex + "-----------菜单下标")
    if (this.data.menuIndex == 1) {
      this.setData({
        tab: [false, false, false, false],
        maskFlag: false,
        historyList: [],
        PageSize: 10, //单页数据条数
        CurrentPage: 1, //当前页码
        scrollTop: 0,
        "SysGuid": this.data.SysGuid,
        "Sys_TypeGuid": this.data.Sys_TypeGuid,
        "ClassifyGuid": this.data.ClassifyGuid,
        noMore: false,
        showIs: false
      })
    } else if (this.data.menuIndex == 2){
      this.setData({
        tab: [false, false, false, false],
        maskFlag: false,
        historyList: [],
        PageSize: 10, //单页数据条数
        CurrentPage: 1, //当前页码
        scrollTop: 0,
        "TaskState": this.data.TaskState,
        "SysGuid": this.data.SysGuid,
        "Sys_TypeGuid": this.data.Sys_TypeGuid,
        "ClassifyGuid": this.data.ClassifyGuid,
        noMore: false,
        showIs: false
      })
    } else if (this.data.menuIndex == 0){
      this.setData({
        tab: [false, false, false, false],
        maskFlag: false,
        historyList: [],
        PageSize: 10, //单页数据条数
        CurrentPage: 1, //当前页码
        scrollTop: 0,
        "EquipmentLocations": this.data.EquipmentLocations,
        "SysGuid": this.data.SysGuid,
        "Sys_TypeGuid": this.data.Sys_TypeGuid,
        "ClassifyGuid": this.data.ClassifyGuid,
        noMore: false,
        showIs: false
      })
    } else {
      this.setData({
        tab: [false, false, false, false],
        maskFlag: false,
        historyList: [],
        PageSize: 10, //单页数据条数
        CurrentPage: 1, //当前页码
        scrollTop: 0,
        "SysGuid": "",
        "Sys_TypeGuid": "",
        "ClassifyGuid": "",
      })
    }
   
    this.initData();
  }, 
  
  // 获取设备类型 1级 获取位置列表
  GetMasterSysPosition: async function () {
    let Param = {
      ParentGuid: "0"
    }
    let data = await utils.get(httpRequest.apiInfo.GetMasterSysPositionApi + "?" + CryptoJS.AesEncrypt(Param))
    console.log("设备类型" + JSON.stringify(data))
    this.setData({
      typeList: data.data.SystemType,
      EquipmentLocation: data.data.Location,
    })
  }, 


  //选择1级  获取设备类型 二级
  selectone: async function ( e) {
    this.setData({
      SysGuid: [],
      Sys_TypeGuid: [],
      ClassifyGuid: [],
      typeList2: [],
      typeList3: [],
    })
    let ParentGuid = e.currentTarget.dataset.rowguid;
    this.setData({
      SysGuid: ParentGuid
    })
    let Param = {
      ParentGuid: ParentGuid
    }
    console.log("设备类型一级参数" + JSON.stringify(Param))
    let data = await utils.get(httpRequest.apiInfo.GetMasterSysPositionApi + "?" + CryptoJS.AesEncrypt(Param))
    console.log("设备类型二级" + JSON.stringify(data.data.SystemType))
    this.setData({
      typeList2: data.data.SystemType
    })
  }, 

  //选择2级  获取设备类型 3级
  selecttwo: async function (e) {
    this.setData({
      Sys_TypeGuid: [],
      ClassifyGuid: [],
      typeList3: [],
    })
    let ParentGuid = e.currentTarget.dataset.rowguid;
    this.setData({
      Sys_TypeGuid: ParentGuid
    })
    let Param = {
      ParentGuid: ParentGuid
    }
    console.log("设备类型二级参数" + JSON.stringify(Param))
    let data = await utils.get(httpRequest.apiInfo.GetMaterClassifyDictionaryApi + "?" + CryptoJS.AesEncrypt(Param))
    console.log("设备类型三级" + JSON.stringify(data.data.SystemType))
    this.setData({
      typeList3: data.data
    })
  }, 


  //选择3级  
  selectthree: async function (e) {
    let ParentGuid = e.currentTarget.dataset.rowguid;
    this.setData({
      ClassifyGuid: ParentGuid
    })
  }, 
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    this.setData({
      screenTop: (app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2,
      customHeight: ((app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 130
    })
    // 获取设备类型
    // this.GetMasterSysDictionary()

    this.GetMasterSysPosition()

    this.refreshView = this.selectComponent("#refreshView")

    //默认本月 的 起止日期
    let date = new Date();
    let yearCurrent = date.getFullYear();
    let yue = date.getMonth() + 1;
    let monthCurrent="";
    // console.log("月"+yue)
    if (yue < 10) {
      monthCurrent = '0' + yue
      console.log(monthCurrent)
    } else {
      monthCurrent = yue
    }
    //  获取当月最后一天
    let lastDay = new Date(yearCurrent, monthCurrent, 0);  
    // console.log(lastDay)
    let day = lastDay.getDate();
    // console.log(day)
    let startYear = yearCurrent + '/' + monthCurrent + '/' + '01'
    let endYear = yearCurrent + '/' + monthCurrent + '/' + day
    this.setData({
      StartTime: startYear,
      EndTime: endYear
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

    this.setData({
      historyList: [],
      PageSize: 10, //单页数据条数
      CurrentPage: 1, //当前页码
    })

    console.log(this.data.StartTime + "--------------任务开始日期")
    console.log(this.data.EndTime + "--------------任务结束日期")
  


    if (this.data.startDate && this.data.FinishDate){
      console.log("从时间选择页面返回查询数据")
      //从时间选择页面返回查询数据
      console.log(this.data.startDate + "--------------任务开始日期")
      console.log(this.data.FinishDate + "--------------任务结束日期")
      this.data.StartTime = this.data.startDate;
      this.data.EndTime = this.data.FinishDate;
    }
    //历史任务列表数据
    this.initData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  //触摸开始
  handletouchstart: function(event) {
    this.refreshView.handletouchstart(event)
  },
  //触摸移动
  handletouchmove: function(event) {
    this.refreshView.handletouchmove(event)
  },
  //触摸结束
  handletouchend: function(event) {
    this.refreshView.handletouchend(event)
  },
  //触摸取消
  handletouchcancel: function(event) {
    this.refreshView.handletouchcancel(event)
  },
  //页面滚动
  onPageScroll: function(event) {
    this.refreshView.onPageScroll(event)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    setTimeout(() => {
      var that = this
      that.setData({
        PageSize: 10, //单页数据条数
        CurrentPage: 1, //当前页码
        historyList: [], //历史任务集合
        scrollTop: 0,
        EquipmentLocations:"",
        "SysGuid": "",
        "Sys_TypeGuid": "",
        "ClassifyGuid": "",
      })
      that.initData();
      this.refreshView.stopPullRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})